<template>
	<view class="content">
		<!-- <image src="../../static/index/bg.png" class="bg" mode=""></image> -->
		<view class="header">
			<view class="title">订单列表</view>
			<scroll-view scroll-x="true" style="white-space: nowrap; ">
				<view class="btns">
					<view class="btn" v-for="(i,index) in ordertab" @click="choose(i)"
						:class="type==i.type?'act':'btn'">{{i.name}}</view>
				</view>
			</scroll-view>
		</view>
		<scroll-view scroll-y="true" class="main" @scrolltolower="more">
			<view class="item" v-for="item in list" :key="item.id" @click="toDetails(item.id)">
				<view class="title">
					<text class="label">{{item.catename}}</text>
					<text v-if="item.status == 0">待付款</text>
					<text v-else-if="item.status == 1 ">待出发</text>
					<text v-else-if="item.status == 2">已出发</text>
					<text v-else-if="item.status == 3">已到达</text>
					<text v-else-if="item.status == 4">开始服务</text>
					<text v-else-if="item.status == 5">已完成</text>
					<text v-else-if="item.status == 6">已评价</text>
					<text v-else-if="item.status == -1">已取消</text>
					<text v-else-if="item.status == -2">待补单</text>
					<text v-else-if="item.status == -3">退款中</text>
				</view>
				<view class="box1">
					<view class="box2">
						<view class="line line1">
							<text class="label">服务技师</text>
							<view class="r">
								<image class="avatar" :src="$oss(item.work_image)" mode=""></image>
								<text class="val">{{item.orderdetail.servicename}}</text>
							</view>
						</view>
						<view class="line line2">
							<text class="label">预约时间</text>
							<view class="r">
								<text class="val">{{item.orderdetail.starttime | date('yyyy-mm-dd hh:MM')}}</text>
							</view>
						</view>
					</view>
					<view class="box3">
						<view class="l">
							<view class="btn btn1" @tap.stop="pay(item.id)" v-if="item.status == 0">立即付款</view>
							<view class="btn btn1" @tap.stop="comment(item)" v-if="item.status == 5">评价</view>
							<view class="btn btn1" @tap.stop="again(item)" v-if="item.status == -1 || item.status == 6">再次购买</view>
							<view class="btn btn1" @tap.stop="repair(item)" v-if="item.status == -2">补单</view>
							<view class="btn" v-if="item.status == 0" @tap.stop="cancelpay(item.id)">取消订单</view>
							<view class="btn" v-if="item.status > 0 && item.status < 4" @tap.stop="cancel(item)">申请退款</view>
						</view>
						<view class="r">
							<!-- <text>金额</text> -->
							<view class="m">
								<text class="fuhao">¥</text>
								<text class="money">{{item.payprice}}</text>
							</view>
						</view>
					</view>
				</view>

			</view>
			<u-empty mode="list" v-if="finish && list.length === 0" style="margin-top: 200rpx;" icon="/static/none.png">
					</u-empty>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				ordertab: [{name: '全部',type: ''},{name: '进行中',type: '1'},{name: '待补单',type: '-2'},{name: '已完成',type: '5'},{name: '已评价',type: '6'}],
				current: 0,
				page: 1,
				finish: false,
				list: [],
				type: ''
			}
		},
		onLoad() {

		},
		onShow() {
			if (uni.getStorageSync('token')) {
				this.reload()
			} else {
				this.finish = true
			}
		},
		methods: {
			cancel(item){
				let that = this
				uni.showModal({
					title: '温馨提示',
					content: '确定要取消该订单吗？',
					success: function (res) {
						if (res.confirm) {
							that.cancels(item.id)
						}
					}
				});
				return
				
				if(item.status == 1) {
					uni.showModal({
						title: '温馨提示',
						content: '确定要取消订单吗？',
						success: function (res) {
							if (res.confirm) {
								that.cancels(item.id)
							}
						}
					});
				} else if((item.status == 2 ||item.status == 3) && item.traveltype === 0) {
					uni.showModal({
						title: '理疗师已出发',
						content: '取消订单将扣除车费',
						success: function (res) {
							if (res.confirm) {
								that.cancels(item.id)
							}
						}
					});
				} else if((item.status == 2 || item.status == 3) && item.traveltype === 1) {
					this.$api.post('/refundprice', {
						id: item.id
					}).then(res => {
						if (res.code == 1) {
							uni.showModal({
								title: '理疗师已出发',
								content: '取消订单将扣除'+res.data+'元',
								success: function (res) {
									if (res.confirm) {
										that.cancels(item.id)
									}
								}
							});
						}
					}).catch((res) => {
					
					})
				} else {
					this.$toast('无法申请退款')
				}
				
			},
			cancels(id){
				this.$api.post('/cancel', {
					id: id
				}).then(res => {
					if (res.code == 1) {
						this.$toast(res.msg)
						this.reload()
					}
				}).catch((res) => {
				
				})
			},
			comment(item){
				this.$goTo('/pages_order/serviceEvaluation/serviceEvaluation?id='+item.id+'&name='+item.orderdetail.servicename+'&avatar='+item.work_image)
			},
			again(item) {
				this.$goTo('/pages/reservation/reservation?cate_id='+item.cate_id+'&id='+item.service_id)
			},
			repair(item) {
				this.$goTo('/pages_order/orderrepair/orderrepair?cate_id='+item.cate_id+'&id='+item.service_id+'&order_id='+item.id+'&num='+item.num)
			},
			cancelpay(id){
				this.$api.post('/cancelpay', {
					id: id
				}).then(res => {
					if (res.code == 1) {
						this.$toast(res.msg)
						this.reload()
					}
				}).catch((res) => {
				
				})
			},
			pay(id){
				let that = this
				this.$api.post('/pay', {
					id: id
				}).then(res => {
					if (res.code == 1) {
						that.$pay(res.data).then(res => {
							
						})
					}
				}).catch((res) => {
				
				})
			},
			choose(item) {
				this.type = item.type
				this.reload()
			},
			reload() {
				this.page = 1
				this.finish = false
				this.getList()
			},
			more() {
				this.page++
				this.getList()
			},
			getList() {
				if (this.finish) {
					return
				}
				let data = {
					page: this.page,
					type: this.type
				}

				this.$api.post('/myorder', data).then(res => {
					if (this.page === 1) {
						this.list = []
					}
					this.list = this.list.concat(res.data)
					if (res.data.length === 0) {
						this.finish = true
					}
				})
			},
			toDetails(id) {
				uni.navigateTo({
					url: '/pages_order/orderDetails/orderDetails?id='+id
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F7F7FA;
	}

	.content {
		height: calc(100vh - var(--window-bottom) - var(--window-top));
		padding-top: var(--status-bar-height);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		.bg{
			position: absolute;
			left: 0;
			top: 0;
			width: 750rpx;
			height: 486rpx;
		}
	}

	.main {
		flex: 1;
		overflow: hidden;
	}

	.header {
		width: 750rpx;
		background: #FFFFFF;
		border-radius: 0rpx 0rpx 30rpx 30rpx;
		box-sizing: border-box;
		padding: 20rpx 30rpx 32rpx;
		z-index: 999;

		.title {
			font-size: 40rpx;
			color: #1C274C;
			font-weight: bold;
			padding: 0 0 40rpx;
		}

		.btns {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.btn {
				font-size: 28rpx;
				padding: 8rpx 28rpx;
				color: #8D93A5;
			}

			.act {
				padding: 8rpx 32rpx;
				background: #00CE9B;
				border-radius: 20rpx;
				color: #fff;
			}
		}
	}

	.main {
		// padding: 32rpx 0;
		z-index: 10;

		.item {
			background: #FBFBFE;
			border-radius: 20rpx;
			margin: 20rpx;

			.title {
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-size: 26rpx;
				color: #1C274C;
				box-sizing: border-box;
				padding: 24rpx 32rpx;

				.label {
					font-size: 28rpx;
					font-weight: 600;
					color: #1C274C;
				}
			}

			.box1 {
				box-sizing: border-box;
				padding: 20rpx;
				background: #FFFFFF;
				border-radius: 20rpx;

				.box2 {
					background: #F6F7FB;
					border-radius: 20rpx;
					padding: 32rpx;
					box-sizing: border-box;
					margin: 0 auto;

					.line {
						display: flex;
						align-items: center;
						justify-content: space-between;

						.label {
							font-size: 26rpx;
							color: #8D93A5;
						}

						.val {
							font-size: 26rpx;
							color: #1C274C;
						}
					}

					.line1 {
						margin-bottom: 30rpx;

						.r {
							display: flex;
							align-items: center;

							.avatar {
								width: 50rpx;
								height: 50rpx;
								border-radius: 50%;
								margin-right: 10rpx;
							}
						}
					}
				}

				.box3 {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 30rpx;

					.l {
						display: flex;
						align-items: center;

						.btn {
							width: 150rpx;
							height: 56rpx;
							border-radius: 28rpx;
							color: #8D93A5;
							font-size: 22rpx;
							border: 1rpx solid #8D93A5;
							display: flex;
							align-items: center;
							justify-content: center;
						}

						.btn1 {
							border: 1rpx solid #342D3B;
							background: #342D3B;
							margin-right: 20rpx;
							color: #F4C889;
						}
					}

					.r {
						font-size: 24rpx;
						color: #8D93A5;
						display: flex;
						align-items: center;

						.m {
							color: #E91919;

							.fuhao {
								font-size: 24rpx;
								margin: 0 15rpx;
							}

							.money {
								font-size: 44rpx;
								font-weight: bold;
							}
						}
					}
				}
			}
		}
	}
</style>